package com.zhou.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhou.dto.Result;
import com.zhou.dto.Weather;
import com.zhou.dto.WeatherYaYa;
import com.zhou.service.WeatherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author lcz
 * @desc 天气服务impl
 * @mail lcz_0130@163.com
 * @date 2023/12/28
 */

@Service
@Slf4j
public class WeatherServiceImpl implements WeatherService {

    /**
     * 获取百度天气
     *
     * @param map 请求参数
     * @return {@link Result}
     */
    @Override
    public Result getBaiDuWeather(Map<String, String> map) {
        RestTemplate restTemplate = new RestTemplate();
        String res = restTemplate.getForObject("https://api.map.baidu.com/weather/v1/?district_id={district_id}&data_type=all&ak={ak}", String.class, map);
        JSONObject json = JSONObject.parseObject(res);

        Result result = new Result();
        if (json == null) {
            //接口地址有误或者接口没调通
            result.setCode("500");
            result.setMessage("接口不通,请检查接口地址!");
            return result;
        }
//            获取接口响应状态
        if (!"0".equals(json.getString("status"))) {
//            响应状态不为0即调用出错
            String err = json.getString("message");
            result.setCode("500");
            result.setMessage("天气接口调用报错:" + err);
            return result;
        }
        JSONArray forecasts = json.getJSONObject("result").getJSONArray("forecasts");
        List<Weather> weathers = forecasts.toJavaList(Weather.class);
        Weather weather = weathers.get(0);
        JSONObject now = json.getJSONObject("result").getJSONObject("now");
        JSONObject location = json.getJSONObject("result").getJSONObject("location");
        weather.setText_now(now.getString("text"));
        weather.setTemp(now.getString("temp"));
        weather.setCity(location.getString("city"));
        weather.setCityName(location.getString("name"));
        weather.setRh(now.getString("rh"));
        result.setCode("200");
        result.setData(weather);
        return result;
    }

    /**
     * 获取丫丫天气
     *
     * @param map 请求参数
     *            城市ID示例：CHBJ000000
     *            IP地址示例：220.181.111.86
     *            经纬度示例：39.93,116.40 （格式为：纬度:经度）
     * @return {@link String}
     */
    @Override
    public Result getYaYaWeather(Map<String, String> map) {
        RestTemplate restTemplate = new RestTemplate();
        String res = restTemplate.getForObject("http://api.yytianqi.com/forecast7d?city={city}&key={key}", String.class, map);
        JSONObject json = JSONObject.parseObject(res);

        Result result = new Result();
        if (json == null) {
            //接口地址有误或者接口没调通
            result.setCode("500");
            result.setMessage("接口不通,请检查接口地址!");
            return result;
        }
//            获取接口响应状态
        if (!"1".equals(json.getString("code"))) {
//            响应状态不为0即调用出错
            String err = json.getString("directions");
            result.setCode("500");
            result.setMessage("天气接口调用报错:" + err);
            return result;
        }
        JSONObject data = json.getJSONObject("data");
        JSONArray list = data.getJSONArray("list");
        List<WeatherYaYa> weathers = list.toJavaList(WeatherYaYa.class);
        WeatherYaYa WeatherYaYa = weathers.get(0);
        WeatherYaYa.setCityName(data.get("cityName").toString());
        JSONObject data2 = Objects.requireNonNull(JSONObject.parseObject(restTemplate.getForObject("http://api.yytianqi.com/observe?city={city}&key={key}", String.class, map))).getJSONObject("data");
        WeatherYaYa.setTq(data2.get("tq").toString());
        WeatherYaYa.setQw(data2.get("qw").toString());
        WeatherYaYa.setFl(data2.get("fl").toString());
        WeatherYaYa.setFx(data2.get("fx").toString());
        WeatherYaYa.setSd(data2.get("sd").toString());
        result.setCode("200");
        result.setData(WeatherYaYa);
        return result;
    }
}
